<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/3/12
 * Time: 15:20
 */

namespace Api\Domain;

use Admin\Api\FreightTemplateApi;
use Shop\Api\AddressApi;
use Shop\Api\ProductApi;
use Shop\Api\ShoppingCartApi;

class FreightDomain extends BaseDomain{

    protected $business_code = 'freight';

    protected $allowType = array("json", "rss", "html");


    /**
     * 计算运费
     */
    public function cost(){
        if(IS_POST){
            $this->checkVersion(100);
            addLog("Freight/cost", $_GET, $_POST, $this->client_id . "调用运费计算接口!");

            $cart_ids = $this->_post('cart_ids','','购物车ID缺失');
            $address_id = $this->_post('address_id','','地址ID缺失');
            $uid = $this->_post('uid','','用户ID缺失');

            $cart_ids = explode(',',$cart_ids);
            $freight_price = 0;

            // foreach($cart_ids as $cart_id){
            //     $result = $this->calc($cart_id,$address_id,$uid);
            //     if($result['status']){
            //         $freight_price += $result['info'];
            //     }else{
            //         $this->apiReturnErr($result['info']);
            //     }

            // }
            //
            $result = $this->groupWithPid($cart_ids);
            $info = $result['info'];
            foreach($info as $val){
                $result = $this->calc($val,$address_id,$uid);
                if($result['status']){
                    $freight_price += $result['info'];
                }else{
                    $this->apiReturnErr($result['info']);
                }
            }
            $this->apiReturnSuc($freight_price);
        }else{
            $this->apiReturnErr('只支持POST请求');
        }

    }


    /**
     * 根据商品ID与件数计算运费
     */
    // public function calc($cart_id = 0, $address_id = 0,$uid = 0){
    public function calc($info = array(), $address_id = 0,$uid = 0){

        //购物车方式结算
        //查询购物车信息
        $p_id = 0;
        $weight = 0;
        $template_id = 0;
        $template_uid = 0;
        $count = 0;

        foreach($info as $val){
            $p_id = $val['p_id'];
            if($val <= 0){
                addLog('Freight/cost',$_GET,$_POST,'购物车id:'.$val['id'].'商品数量必须大于0',true);
                return array('status'=>false,'info'=>'商品购买数量必须大于0');
            }
            if($val['count']>0)$count += $val['count'];
        }
        // $result = apiCall(ShoppingCartApi::GET_INFO,array(array('id'=>$cart_id)));
        // if($result['status']){

        //     if(!is_null($result['info'])){
        //         $p_id = $result['info']['p_id'];
        //         $count = $result['info']['count'];
        //         if($count <= 0){
        //             addLog('Freight/cost',$_GET,$_POST,'购物车id:'.$cart_id.'商品数量必须大于0',true);
        //             return array('status'=>false,'info'=>'商品购买数量必须大于0');
        //         }

        //     }else{
        //         addLog('Freight/cost',$_GET,$_POST,'购物车id:'.$cart_id.'数据不存在',true);
        //         return array('status'=>false,'info'=>'购物车id:'.$cart_id.'数据不存在');
        //     }

        // }else{
        //     return array('status'=>false,'info'=>$result['info']);
        // }

        $map = array(
            'id'=>  $p_id
        );


        $result = apiCall(ProductApi::GET_INFO,array($map));

        if($result['status']){
            if(!is_null($result['info'])){
                //查询商品价格
                $info = $result['info'];
                $template_id = $info['template_id'];
                $weight = $info['weight'];
                $template_uid = $info['uid'];//运费模版主人ID
            }else{
                addLog('Freight/cost',$_GET,$_POST,'商品id:'.$p_id.'数据不存在',true);
                return array('status'=>false,'info'=>'商品id:'.$p_id.'数据不存在');
            }


            //查询运费模版
            $template_info = array();
            $freightAddress = array();
            if($template_id!=0){
                $map = array(
                    'uid'=>$template_uid,
                    'id'=>$template_id
                );
                $result = apiCall(FreightTemplateApi::FIND_TEMPLATE,array($map));

                if($result['status']){
                    if(!is_null($result['info'])){
                        $template_info = $result['info'];
                        $freightAddress = $result['info']['freightAddress'];
                    }else{
                        addLog('Freight/cost',$_GET,$_POST,'运费模版id:'.$template_id.'数据不存在',true);
                        return array('status'=>false,'info'=>'运费模版id:'.$template_id.'不存在');
                    }
                }else{
                    return array('status'=>false,'info'=>$result['info']);
                }


                //查询收获地址信息
                $address_info = 0;
                $map = array(
                    'uid'=>$uid,
                    'id'=>$address_id
                );

                $result = apiCall(AddressApi::GET_INFO,array($map));
                if($result['status']){
                    if(!is_null($result['info'])){
                        $address_info = $result['info'];
                    }else{
                        addLog('Freight/cost',$_GET,$_POST,'收货地址id:'.$address_id.'数据不存在',true);
                        return array('status'=>false,'info'=>'收货地址id:'.$address_id.'数据不存在');
                    }
                }else{
                    return array('status'=>false,'info'=>$result['info']);
                }

                //计算运费
                $add_id = 0;
                if($address_info['city'] == '市辖区'|| $address_info['city'] == '县'){
                    //使用省ID去查询
                    $add_id = $address_info['provinceid'];
                }else{
                    $add_id = $address_info['cityid'];
                }

                //遍历运费模版
                foreach($freightAddress as $key=>$val){

                    //addressids为0是全国默认运费
                    if($val['addressids']==0){
                        $default_freight_key = $key;
                    }else{
                        //分割地址组数组
                        $addids = explode(',',$val['addressids']);
                        //判断用户收货地址是否在地址组中
                        if(in_array($add_id,$addids)){
                            $freight_key = $key;
                            break;
                        }
                    }

                }

                if(!isset($default_freight_key)){
                    addLog('Freight/cost',$_GET,$_POST,'运费模版id:'.$template_id.'没有全国默认运费',true);
                    return array('status'=>false,'info'=>'运费模版id:'.$template_id.'没有全国默认运费');
                }


                if(!isset($freight_key)){
                    //没有局部运费，则使用全国运费
                    $freight_info = $freightAddress[$default_freight_key];
                }else{
                    $freight_info = $freightAddress[$freight_key];
                }


                //处理运费计算根据件数重量
                $type = $template_info['type'];//1:件数；2：重量;3:体积
                $count_c = 0;//单位
                $firstpiece = $freight_info['firstpiece'];
                $fistmoney = $freight_info['firstmoney'];
                $replenishpiece = $freight_info['replenishpiece'];
                $replenishmoney = $freight_info['replenishmoney'];
                $freight = 0;
                if($type==1){

                    $count_c = $count;

                }
                elseif($type==2){

                    $count_c = $count * $weight;

                }

                //计算首件价格
                $freight = $freight +$fistmoney;

                //如果超过首件，计算续件价格
                if($count_c > $firstpiece){

                    $freight = $freight + ceil(($count_c - $firstpiece) / $replenishpiece) * $replenishmoney;

                }

                return array('status'=>true,'info'=>$freight);//返回价格


            }else{
                addLog('Freight/cost',$_GET,$_POST,'商品id:'.$p_id.'运费模版ID为0',true);
                return array('status'=>false,'info'=>'商品id:'.$p_id.'运费模版ID为0');
            }


        }else{
            return array('status'=>false,'info'=>$result['info']);
        }


    }

    private function groupWithPid($cart_ids = array()){
        $map = array(
            'id' => array('in',$cart_ids)
        );
        $result = apiCall(ShoppingCartApi::QUERY_NO_PAGING,array($map));
        if(!$result['status']){
            return array('status'=>false,'info'=>$result['info']);
        }
        $data = array();
        foreach($result['info'] as $val){
            if(!isset($data[$val['p_id']])) $data[$val['p_id']] = array();
            array_push($data[$val['p_id']],$val);
        }
        return array('status'=>true,'info'=>$data);
    }


}